package services

import (
	"fmt"
	"gin_template/models"

	"github.com/go-basic/uuid"
)

func Login(userModel *models.AuthUser) (*models.User, error) {
	var option = models.GetUserRequest{Account: userModel.Account}
	if userList, err := models.FindUserList(&option); err != nil {
		return nil, err
	} else {
		if userList.Total <= 0 { //查无此人
			return nil, fmt.Errorf("查无此人")
		}
		user := userList.UserList[0]
		// 一般情况下，此处需要加密
		if user.Password == userModel.Password {
			return user, nil
		} else {
			return nil, fmt.Errorf("密码输入错误")
		}
	}
}

func Register(userModel *models.User) (string, error) {
	var option = models.GetUserRequest{Account: userModel.Account}
	if userList, err := models.FindUserList(&option); err != nil {
		return "", err
	} else {
		if userList.Total > 0 { //此账号名已被注册过
			return "", fmt.Errorf("此账号名已被注册")
		}
		userModel.UserId = uuid.New()
		if userId, err1 := userModel.InsertOne(); err1 != nil {
			return "", err1
		} else {
			return userId, nil
		}
	}
}
